* DiD OF INMOVERS TO OW

* MASTER-DO-FILE, running all the do-files to produce the results of the inmovers

/* Results produced:
- Table 3: input needed for summary table (Columns 1-3)
- Figure 6b): Event Study graph
- Figure 7b): tax rate changes
- Table 4: elasticity estimates (Panel B) (results written to Excel)
*/

// NOTE: data needed for this do-file cannot be shared due to privacy reasons



version 16.1

clear all
cap log close 
cap clear matrix
set more off
cap set scheme mygraphs

local rescue_mypathRR "$mypathRR"


* get the data with all the indicators&dummies needed
use "/Volumes/OW_data/my_dta_v8.1.dta", clear
label var zuzugjahre "move-in year (including several moves)"
label var zuzugjahr "move-in year (last move)"
drop if year>2010
drop if zuzugjahre>2010
bys year: egen residents=count(persid)

drop if zuzugkt==6
drop if zuzugjahre < 2001

* Adjust taxable income to account for the changes in deduction in 2008
// to account for the increased deduction under the flat-tax regime
replace esteuerbst=esteuerbst+10000 if year>=2008 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

cd "$mypathRR"

* GENERATE THE TREATMENT DEFINITION BASED ON RATE DETERMINING INCOME
gen taxinc_thre2 = (esatzbestkopf>= 300000 & esatzbestkopf <.)
label var taxinc_thre2 "Taxable income threshold regressive tax"

gen ctrl_ir_55_75 = (esatzbestkopf>= 300000*0.55 & esatzbestkopf< 300000*0.75)
label var ctrl_ir_55_75 "Control group: 55-75% of income threshold"

gen ctrl_ir_60_80 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.80)
label var ctrl_ir_60_80 "Control group: 60-80% of income threshold"

gen ctrl_ir_60_85 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.85)
label var ctrl_ir_60_85 "Control group: 60-85% of income threshold"

gen ctrl_ir_60_95 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.95)
label var ctrl_ir_60_95 "Control group: 60-95% of income threshold"

gen ctrl_ir_45_70 = (esatzbestkopf>= 300000*0.45 & esatzbestkopf< 300000*0.70)
label var ctrl_ir_45_70 "Control group: 40-70% of income threshold"

gen taxwea_thre = (vsatzbestkopf>= 5000000 & vsatzbestkopf <.)
label var taxwea_thre "Taxable wealth threshold regressive tax"

gen ctrl_wea = (vsatzbestkopf>= 3500000 & vsatzbestkopf< 4900000)
label var ctrl_wea "Control group: 3.5-4.9 million in taxable wealth"

gen treat_double = (taxwea_thre == taxinc_thre2 == 1)
label var treat_double "Treated by regressive wealth and income tax"

gen control_double = (ctrl_ir_60_80 == ctrl_wea == 1)
label var control_double "Control group: below income and wealth thresholds"


* Generate a dummy for each bracket
gen regbr1		= (esatzbestkopf>=300001 & esatzbestkopf<=340000)
gen regbr2		= (esatzbestkopf>=340001 & esatzbestkopf<=380000)
gen regbr3		= (esatzbestkopf>=380001 & esatzbestkopf<=420000)
gen regbr4		= (esatzbestkopf>=420001 & esatzbestkopf<=550000)
gen regbr5		= (esatzbestkopf>=550001 & esatzbestkopf<=600000)
gen regbr6		= (esatzbestkopf>=600001 & esatzbestkopf<=1000000)
gen regbr7		= (esatzbestkopf>=1000001 & esatzbestkopf<. )




* DEFINE WORKERS / COMMUTERS / RETIREES

* 1) Gen dummies for retirees
gen retiree01_1 = 1
foreach var in z100s1 z101s1 z104s1 z105s1 z110s1 z111s1 z114s1 z115s1 {
	replace retiree01_1 = 0 if `var' > 0 & `var' <.
}

gen retiree01_1dt  = 1
foreach var in z100s1  z104s1 z110s1  z114s1   {
	replace retiree01_1dt = 0 if `var' > 0 & `var' <.
}
gen retiree01_1ep = 1
foreach var in  z101s1  z105s1  z111s1  z115s1 {
	replace retiree01_1ep = 0 if `var' > 0 & `var' <.
}


gen retiree01_2 = 0
foreach var in z130s1 z131s1 z132s1 z133s1 z134s1 z135s1 {
	replace retiree01_2 = 1 if `var' > 0 & `var' <.
}

gen retiree01_2dt = 0
foreach var in z130s1  z132s1  z134s1  {
	replace retiree01_2dt = 1 if `var' > 0 & `var' <.
}
gen retiree01_2ep = 0
foreach var in  z131s1  z133s1 z135s1 {
	replace retiree01_2ep = 1 if `var' > 0 & `var' <.
}

gen retiree01_2sp = retiree01_2dt * retiree01_2ep

gen retiree01_3 = retiree01_2 * retiree01_1

label var retiree01_1 "Both partners are retirees: no labor income in couple"
label var retiree01_2 "At least one partner is retiree: some pension income in couple"
label var retiree01_2sp "Both partners are retirees: both have pension income each"
label var retiree01_3 "Retiree household with pension income and no labor income"

* 2) Gen dummies for working
gen inlf01_1 = (retiree01_1 ==0)
	label var inlf01_1 "Household has some labor income in couple"
gen inlf01_1dt = (retiree01_1dt ==0)
	label var inlf01_1dt "DT has some labor income"
gen inlf01_1ep = (retiree01_1ep ==0)
	label var inlf01_1ep "Spouse has some labor income"
	

* 3) Gen dummies for working outside of the canton
// ass 1: people who pay for annual transport pass commute out of canton
gen oev01_dt = (z201s1 > 1300 & z201s1 < .)
gen oev01_ep = (z202s1 > 1300 & z202s1 < .)
gen oev01 = (oev01_dt == 1 | oev01_ep == 1)

// ass 2: people who drive > 25km commute out of canton (distance Sarnen Lucerne)
/*  km x 2 x days of work per year == 222 x 0.7 CHF
	25 * 2 * 220 * 0.7 = 7770 CHF
	for spouse: if they work 50%, deduction will be 3885 CHF
*/
gen auto01_dt = (z205s1 > 7700 & z205s1 < .)
gen auto01_ep = (z206s1 > 3850 & z206s1 < .)
gen auto01 = (auto01_dt == 1 | auto01_ep == 1)

gen commuter01 = (auto01 == 1 | oev01 ==1 )

* 4) tax rate differentials

* 4.1) get indicator of whether origin municipalities are low or high-tax
	frame create fr_taxrates
	frame change fr_taxrates

	use "$mypathRR/Datasets/taxload_municipalities.dta", clear

	keep gdenr gdename kannr kanton jahr stfuss stfuss_q stfuss_ek stfuss_kk konfession stb_q estvsample einkst_v0k_175 einkst_v0k_200 einkst_v0k_250 einkst_v0k_300 einkst_v0k_400 einkst_v0k_500 einkst_v0k_1000

	rename jahr year
	keep if year >2000 & year <2011


	sum einkst_v0k_200 einkst_v0k_250 einkst_v0k_300 einkst_v0k_400 einkst_v0k_1000, de

	foreach var in 200 250 300 400 1000 {
		gquantiles taxcat_`var' = einkst_v0k_`var', nq(4) xtile by(year)
	}

	frame change default

	frlink m:1 year zuzugvonbfs, frame(fr_taxrates year gdenr) gen(gdlink)

	frget taxcat_*, from(gdlink)

* 4.2) gen tax rate indicator
// low tax
foreach var in 200 300 1000 {
gen lowtax_`var'= (taxcat_`var' == 1)
replace lowtax_`var'= . if taxcat_`var' == .
}
gen lowtax_sum = 0
foreach var in 200 300 1000 {
replace lowtax_sum = lowtax_sum + lowtax_`var'
}
sum lowtax_sum, de
tab lowtax_sum
gen lowtax01 = (lowtax_sum > 1 & lowtax_sum < . )
replace lowtax01 = . if lowtax_sum == . 

// high tax
foreach var in 200  300 1000 {
gen hightax_`var'= (taxcat_`var' == 4)
replace hightax_`var'= . if taxcat_`var' == .
}
gen hightax_sum = 0
foreach var in 200  300 1000 {
replace hightax_sum = hightax_sum + hightax_`var'
}
sum hightax_sum, de
tab hightax_sum
gen hightax01 = (hightax_sum > 1 & hightax_sum <. )
replace hightax01 = . if hightax_sum == . 

tab hightax01 lowtax01, mi


* DEFLATE THE INCOME VARIABLES
merge m:1 year using "$mypathRR/Datasets/CPI.dta"
drop _merge
drop if year==.

foreach var in esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm  taxsavings meantaxsavings z199s1 z450s1 z300s1 z470s1 inmob_inc mob_inc {
replace `var'=`var'/cpi*100.5 //deflate the series
local u : variable label `var'
local l= "`u' (real)" 
label var `var' "`l'"
}


* Generate the net-of-tax rate
gen avg_ntr=1-avgt_stek_taxable
label var avg_ntr "Average net-of-tax rate"
gen avg_ntr1=avg_ntr if year<2006 & zuzugjahre<2006
	label var avg_ntr1 	"Avg. NTR $t<2006$"
gen avg_ntr2=avg_ntr if year>2005 & year<2008 & zuzugjahre>2005 & zuzugjahre<2008
	label var avg_ntr2 	"Avg. NTR $t>=2006^*$"


* * * PREPARE SOME VARIABLES TO PROPERLY IDENTIFY INCOME AND WEALTH IN THE MOVE-IN YEAR AND STILL COUNT THE INMOVERS CORRECTLY
sort persid year
egen freq= tag(persid zuzugjahre) //this will be used to select the inmovers

foreach var in avg_ntr1 avg_ntr2  sex age married doppel snpanzki inlf01_1 hightax01 lowtax01 commuter01 auto01 oev01 z205s1 z206s1 z201s1 retiree01_3 retiree01_1 retiree01_2sp employee self_emp farmer retiree labor_ratio self_ratio cap_ratio inmob_ratio mob_ratio labor_index3 mob_index3 mob_inc labor_inc self_inc  woaufenthalt z220s1 z221s1 z222s1 z223s1 z255s1 z256s1 swiss zuzugausland pauschaliert ///
			esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm taxsavings meantaxsavings z199s1 z450s1 z300s1 z470s1 inmob_inc  taxinc_dez taxinc_thre2 taxwea_thre2 taxwea_dez ctrl_ir_55_75 ctrl_ir_60_80 ctrl_ir_60_85 ctrl_ir_60_95 ctrl_ir_45_70   ctrl_wea taxwea_thre control_double treat_double ///
			regbr1 regbr2 regbr3 regbr4 regbr5 regbr6 regbr7 {
  gen yrz=(year==zuzugjahre)
  replace yrz=yrz*(-1)
  gen `var'_yz=.
  bys persid (year): replace `var'_yz=`var' if yrz==-1
  bys persid _spell (yrz): replace yrz=yrz[1]
  bys persid (year): replace `var'_yz=`var'_yz[_n-1] if yrz==-1 & `var'_yz==.
  
  forvalues z=1/11 {
    local i=`z'
    bys persid (year): replace `var'_yz=`var'_yz[_n+1] if yrz==-1 & `var'_yz==. // note: repeat this step until 0 real changes made
    }
  drop yrz
}

foreach var in avg_ntr1 avg_ntr2  sex age married doppel snpanzki inlf01_1 hightax01 lowtax01 commuter01 auto01 oev01 z205s1 z206s1 z201s1 retiree01_3 retiree01_1 retiree01_2sp employee self_emp farmer retiree labor_ratio self_ratio cap_ratio inmob_ratio mob_ratio labor_index3 mob_index3 mob_inc labor_inc self_inc  woaufenthalt z220s1 z221s1 z222s1 z223s1 z255s1 z256s1 swiss zuzugausland pauschaliert ///
			esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm taxsavings meantaxsavings z199s1 z450s1 z300s1 z470s1 inmob_inc  taxinc_dez taxinc_thre2 taxwea_thre2 taxwea_dez ctrl_ir_55_75 ctrl_ir_60_80 ctrl_ir_60_85 ctrl_ir_60_95 ctrl_ir_45_70   ctrl_wea taxwea_thre control_double treat_double ///
			regbr1 regbr2 regbr3 regbr4 regbr5 regbr6 regbr7 {
			replace `var'=`var'_yz
}





* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* SUMMARY STATISTICS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Inmovers-summary_tables"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* EVENT-STUDY GRAPHS OF DIFF-IN-DIFF
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Inmovers-ES_graphs.do"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DIFF-IN-DIFF INCOMERS - RESULTS FOR PAPER
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Inmovers-DiD.do"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DIFF-IN-DIFF SHARE OF INCOMERS (APPENDIX)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Inmovers-DiD_shares.do"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DESCRIPTIVES ABOUT COMMUTING AND WORKING STATUS OF RICH MOVERS (APPENDIX)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Inmovers-commuters.do"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * MAKE A NICE TABLE SUMMING UP ALL THE RESULTS * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

cd "$mypathRR/Results"

version 14.1
putexcel set "Tab4 - MainResults_formatted.xlsx", modify 

* reduced level (Col 1 = B)
putexcel B27 = "$DiD_60_80_B"
putexcel B28 = "($SE_60_80_B)"
putexcel B35 = $delta_tax_2006_60_80
putexcel B36 = $mean_incomers

* reduced log (Col 2 = C)
putexcel C27 = "$DiD_60_80_C"
putexcel C28 = "($SE_60_80_C)"
putexcel C36 = $lnmean_incomers

* 2SLS avg. tax rate, 2006-07 (Col 3 = D)
putexcel D29 = "$E_60_80_E"
putexcel D30 = "($SE_60_80_E)"
putexcel D31 = "$Ftest_60_80_st"

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel D32 = "${E10_60_80_E}"
putexcel D33 = "(${SE10_60_80_E})"
putexcel D34 = "$Ftest_60_80_st_2tr"



* reduced level (Col 4 = E)
putexcel E27 = "$DiD_55_75_B"
putexcel E28 = "($SE_55_75_B)"
putexcel E35 = $delta_tax_2006_55_75
putexcel E36 = $mean_incomers

* reduced log (Col 5 = F)
putexcel F27 = "$DiD_55_75_C"
putexcel F28 = "($SE_55_75_C)"
putexcel F36 = $lnmean_incomers


* 2SLS avg. tax rate, 2006-07 (Col 6 = G)
putexcel G29 = "$E_55_75_E"
putexcel G30 = "($SE_55_75_E)"
putexcel G31 = "$Ftest_55_75_st"

putexcel G32 = "$E10_55_75_E"
putexcel G33 = "($SE10_55_75_E)"
putexcel G34 = "$Ftest_55_75_st_2tr"



global drop _all
clear frames
global mypathRR "`rescue_mypathRR'"



						* * * * *  E N D  * * * * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
